const SysUserDao = appRequire(`dao/desktop/SysUserSequelizeDao.js`);
const SysUser = appRequire('model/desktop/SysUser');
const ResultVo = appRequire('vo/ResultVo.js');
const Menu = appRequire('vo/index/Menu.js');
const SqlUtil = appRequire('modules/SqlUtil.js');
const uuid = require('uuid');
var dateFormat = require("dateformat");
const SqliteUtil = require('../../modules/SqliteUtil');
class SysUserService {
    constructor() {

    }
    static async save(user) {
        if (user.id === '' || user.id === undefined) {
            user.id = uuid.v1();
            user.createTime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
            user.updateTime = user.createTime;
            await SysUserDao.saveOne(user);
        } else {
            let dbUser = await SysUserDao.findById(user.id);
            console.log(dbUser,user);
            // SqliteUtil.dbObjectToEntity(dbUser,user);
            user.sysUserRoleIds = user.roleIds;
            user.updateTime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
            console.log(user);
            await SysUserDao.updateOne(user);
        }
    }

    static async deleteOne(id) {
        return await SysUserDao.deleteById(id);
    }

    static async deleteMany(ids) {
        ids = JSON.parse(ids);
        return await SysUserDao.deleteMany(ids);
    }

    static async findOne(id) {
        return await SysUserDao.findOne(id);
    }

    static async findUserAuthItem(user) {
        let data = await SysUserDao.findUserAuthItem(user);
        let authMenu = data.authMenus;
        let menuList = [];        
        let menuMap = {};
        authMenu = SqlUtil.formatDBList(authMenu,Menu.config);
        // console.log(authMenu);
        authMenu.forEach(function(val){
            menuMap[val.id] = val;
        });
        let root = null;
        authMenu.forEach(function(val){
            if(!menuMap[val.pid]){
                root = val;
            }    
        });
        // console.log(root);
        authMenu.forEach(function(val){
            if(val.pid==root.pid){
                menuList.push(val);
            }else{
                let p = menuMap[val.pid];
                // console.log(p);
                if(p){
                    if(!p.child){
                        p.child = [];
                    }
                    p.child.push(val);
                }
                
            }
        });
        // console.log(JSON.stringify(menuList));
        data.authMenus = menuList;
        return data;
    }

    static async findUserCommonAuthItem(){
        let authUrls = await SysUserDao.findUserCommonAuthItem();
        authUrls = authUrls.map(function(val){
            return val.authUrl;
        });
        return authUrls;
    }
    
     //递归获取子菜单
    static async findOneByUsernameAndPassword(user) {
        if(user&&user.username&&user.password){
            return await SysUserDao.findOneByUsernameAndPassword(user);
        }else{
            throw new Error("用户名或密码参数不正确!");
        }
    }


    static async findAll() {
        return await SysUserDao.findAll();
    }

    static async findByPage(page) {
        page.pageNum = parseInt(page.pageNum);
        page.pageSize = parseInt(page.pageSize);
        page.searchParams = JSON.parse(page.searchParams);
        return await SysUserDao.findByPage(page);
    }

}
module.exports = SysUserService;